package com.xh.rufongwallpaper.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xh.rufongwallpaper.common.result.Result;
import com.xh.rufongwallpaper.common.constant.Constants;
import com.xh.rufongwallpaper.common.util.VOConverter;
import com.xh.rufongwallpaper.entity.Wallpaper;
import com.xh.rufongwallpaper.service.FavoriteService;
import com.xh.rufongwallpaper.vo.PageResponseVO;
import com.xh.rufongwallpaper.vo.WallpaperVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 收藏控制器
 *
 * @author XH
 */
@RestController
@RequestMapping("/favorites")
@RequiredArgsConstructor
@Tag(name = "收藏API", description = "壁纸收藏相关接口")
public class FavoriteController {

    private final FavoriteService favoriteService;

    /**
     * 获取收藏列表
     */
    @GetMapping
    @Operation(summary = "获取收藏列表", description = "获取设备的收藏壁纸列表")
    public PageResponseVO<WallpaperVO> getFavorites(
            @Parameter(description = "设备ID") @RequestHeader("X-Device-ID") String deviceId,
            @Parameter(description = "页码，从1开始") @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "20") int limit) {
        
        // 将页码转为0-based索引
        int pageIndex = Math.max(0, page - 1);
        int pageSize = Math.min(limit, Constants.MAX_PAGE_SIZE);
        
        // 获取收藏的壁纸
        Page<Wallpaper> wallpaperPage = favoriteService.getFavoriteWallpapers(deviceId, pageIndex, pageSize);
        
        // 转换为前端响应格式
        return VOConverter.convertToPageResponse(wallpaperPage);
    }

    /**
     * 添加收藏
     */
    @PostMapping("/{wallpaperId}")
    @Operation(summary = "添加收藏", description = "添加壁纸到收藏")
    public Result<Boolean> addToFavorites(
            @Parameter(description = "设备ID") @RequestHeader("X-Device-ID") String deviceId,
            @Parameter(description = "壁纸ID") @PathVariable Long wallpaperId) {
        
        boolean success = favoriteService.addFavorite(deviceId, wallpaperId);
        return Result.success(success);
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/{wallpaperId}")
    @Operation(summary = "取消收藏", description = "从收藏中移除壁纸")
    public Result<Boolean> removeFromFavorites(
            @Parameter(description = "设备ID") @RequestHeader("X-Device-ID") String deviceId,
            @Parameter(description = "壁纸ID") @PathVariable Long wallpaperId) {
        
        boolean success = favoriteService.removeFavorite(deviceId, wallpaperId);
        return Result.success(success);
    }

    /**
     * 检查壁纸是否已收藏
     */
    @GetMapping("/check/{wallpaperId}")
    @Operation(summary = "检查壁纸是否已收藏", description = "检查设备是否已收藏指定壁纸")
    public Result<Boolean> checkFavorite(
            @Parameter(description = "设备ID") @RequestHeader("X-Device-ID") String deviceId,
            @Parameter(description = "壁纸ID") @PathVariable Long wallpaperId) {
        
        boolean isFavorite = favoriteService.isFavorite(deviceId, wallpaperId);
        return Result.success(isFavorite);
    }
} 